package org.zebra.mybatis.plus.ext.audit;

import lombok.Data;

import java.util.List;

/**
 * @author zhanghongbin
 */
@Data
public class OperationResult {
    private String operation;
    private boolean recordStatus;
    private String tableName;
    private String changedData;
    /**
     * cost for this plugin, ms
     */
    private long cost;

    public void buildDataStr(List<DataAudit.DataChangedRecord> records) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (DataAudit.DataChangedRecord r : records) {
            sb.append(r.generateUpdatedDataStr()).append(",");
        }
        if (sb.length() == 1) {
            sb.append("]");
            changedData = sb.toString();
            return;
        }
        sb.replace(sb.length() - 1, sb.length(), "]");
        changedData = sb.toString();
    }

    @Override
    public String toString() {
        return "{" + "\"tableName\":\""
                + tableName + "\"," + "\"operation\":\""
                + operation + "\"," + "\"recordStatus\":\""
                + recordStatus + "\"," + "\"changedData\":"
                + changedData + "," + "\"cost(ms)\":"
                + cost + "}";
    }
}
